package org.jaaksi.test.jasonpaser;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import org.jaaksi.test.jasonpaser.bean.Person;
import org.jaaksi.test.jasonpaser.bean.Student;
import org.jaaksi.test.jasonpaser.util.DataUtil;
import org.jaaksi.test.jasonpaser.util.RawUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gson mGson = DataUtil.mGson;
        String conversion = RawUtils.getJsonString(this, R.raw.conversion);

        // Gson和FastJson都可以解析出来，
//        Person person = mGson.fromJson(conversion, Person.class);
//        Log.e("fcy", "----------------- person=" + person);
        Person p2 = JSON.parseObject(conversion, Person.class);
        Log.e("fcy", "----------------- p2=" + p2);
        String s2 = JSON.toJSONString(p2);
        Log.e("fcy","----------------- s2="+s2);


        Person p = new Person();
        Date date = new Date(1000);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
        String format1 = format.format(date);

        // fastjson生成json
        JSONObject jsonObject = new JSONObject();
        // 如果value为null,也会添加，不过toString()时，会吧null忽略掉
        jsonObject.put("id", "200903030041");
        jsonObject.put("name", null);
        jsonObject.put("age", 22);
        jsonObject.put("person", p2);
        String s = jsonObject.toString();
//        Log.e("fcy", "----------------- s=" + s);

        String arrJson = RawUtils.getJsonString(this, R.raw.array);
        List<Student> students = JSON.parseArray(arrJson, Student.class);
//        Log.e("fcy", "----------------- students=" + students);


//        JsonObject parse = (JsonObject) new JsonParser().parse(arrJson);
        JsonArray jsonArray = (JsonArray) new JsonParser().parse(arrJson);
        String s1 = jsonArray.toString();
//        Log.e("fcy", "----------------- s1=" + s1);

//        List<Map<String, Object>> listMap = JSON.parseObject(jsonString,
//            new TypeReference<List<Map<String, Object>>>() {});

    }
}
